#ifndef APP_H
#define APP_H
	#include "gd32e10x.h"
	#include "serial.h"
	#include "led.h"
	#include "adc.h"
	#include "param.h"
	#include "at.h"
	#include "mavlink.h"
	
	#define APP_NAME "SMART BATTERY TEMP CONTROL"
	#define MAIN_VERSION 1
	#define MAIN_VER_NUM 1
	
	#define GET_APP_MSG(M,N) (APP_NAME" V"#M"."#N)
	#define APP_MSG 	GET_APP_MSG(MAIN_VERSION,MAIN_VER_NUM)
	
	extern struct boardinfo board_info;
	
	#define close_heat() gpio_bit_set(GPIOB, GPIO_PIN_7)
	#define open_heat()  gpio_bit_reset(GPIOB, GPIO_PIN_7)
	
	#define close_motor() gpio_bit_set(GPIOB, GPIO_PIN_6)
	#define open_motor()  gpio_bit_reset(GPIOB, GPIO_PIN_6)
	
	typedef struct {
		uint32_t last_heat_temp_time_ms;
		uint32_t last_motor_temp_time_ms;
		uint32_t last_batt_time_ms;
		uint32_t debug_loop_time_ms;
		float cal_vol;
		
		float vol1_cal_sum;		
		float vol2_cal_sum;
		uint8_t  vol_cal_count;
				
		uint8_t batt_low : 1;
		uint8_t heat_enable : 1;
		uint8_t motor_enable : 1;
		uint8_t vol_cal_enable : 1;
		uint8_t debug_level : 2;
	}SystemStatus;
		
	void app_init(void);
	
	void fwdgt_init(void);
	
	void app(void);
	
	void debug_string(const char* s);
	
	void debug_update(void);
	
	void at_data_process(uint8_t* data, uint32_t len);
	
	void led_task(void);
	
	void cal_vol_update(void);
	
	void batt_low_update(void);
	
	void user_timer_init(void);
	
	void heat_control_update(void);
	
	void motor_control_update(void);
	
	void serial_send_msg(const char* s, ...);
	
	void serial_send_string(const char* s);	
	
	void mavlink_init(void);
	
	void mavlink_check(void);
	
	void mavlink_data_process(uint8_t* data, uint32_t len);
	
	void mavlink_send(mavlink_channel_t com, const char *buf, uint16_t len);
	
	extern Led led_run_obj;
	extern uint32_t led_blink_time_ms;
	extern uint32_t led_last_time_ms;
	extern SystemStatus system_status;
	
	extern mavlink_system_t gcs_mavlink_system;
	
#define MAVLINK_TEST 0
#if MAVLINK_TEST
	void mavlink_test(void);
#endif
#endif

